<?php

namespace Admin\Controller;
use Think\Controller;

class MemberController extends BaseController{
	
	public function index(){
		$nickname = I('nickname');
        if($nickname){
            $member = M('user');
            $count = $member->where("nickname like '%{$nickname}%'")->count();
            $page = new \Think\Page($count,10);
            $show = $page->show();
            $list = $member->where("nickname like '%{$nickname}%'")->limit($page->firstRow.','.$page->listRows)->order("id ASC")->select();
        }else{
            $member = M('user');
            $count = $member->count();
            $page = new \Think\Page($count,10);
            $show = $page->show();
            $list = $member->limit($page->firstRow.','.$page->listRows)->order('id ASC')->select();
        }
		
		foreach($list as &$value){
            $value['source']=$this->source($value['source']);
            $value['user_role']=$this->user_role($value['user_role']);
            $value['role_id']=M('role')->where(['id'=>$value['role_id']])->getField("role_name");
            $value['career_id']=M('career')->where(['id'=>$value['career_id']])->getField("title");
            if ($value['company_id']==0){
                $value['company_id']="系统管理员";
            }else{
                $value['company_id']=M('company')->where(['id'=>$value['company_id']])->getField("title");
            }
            $value['last_time']=!empty($value['last_time'])?date('Y/m/d H:i:s',$value['last_time']):0;
            $value['create_time']=date('Y/m/d H:i:s',$value['create_time']);
            //剩余天数
            if ($value['deadline']-time()>0){
                $daysinfo=$value['deadline']-time();
                $days=round($daysinfo/(3600*24));
                $value['deadline']=$days;
            }else{
                $value['deadline']=0;
            }

        }
        
		$this->assign('show',$show);
		$this->assign('list',$list);
		$this->display();
	}

	private function user_role($k){
	    $list[1]="系统管理员";
	    $list[2]="客户管理员";
	    $list[3]="用户";
	    return $list[$k];
    }

	private function source($k){
	    $list[1]="app";
	    $list[2]="推荐";
	    $list[3]="第三方登录";
	    return $list[$k];
    }

    
	public function del(){
		$id = I('id');
		M('user')->where("id = $id")->delete();
//		M('wx')->where("userid = $id")->delete();
//		M('order')->where("userid = $id")->delete();
		M('message')->where("uid = $id")->delete();
//		M('integral')->where("uid = $id")->delete();
//		M('quest')->where("uid = $id ")->delete();
		$this->success("删除成功");
	}
	
	
	public function disable(){
		$id = I('id');
		$res = M('user')->where("id = $id")->setField('status',0);
		if($res){
			$this->success('禁用成功！');
		}else{
			$this->error('禁用失败！');
		}
	}
	
	
	public function endisable(){
		$id = I('id');
		$res = M('user')->where("id = $id")->setField('status',1);
		if($res){
			$this->success('启用成功！');
		}else{
			$this->error('启用失败！');
		}
	}
	
}

?>